package com.yonyou.crm.bpub.detail.service;

import java.io.Serializable;
import java.rmi.server.ObjID;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.approval.entity.ApprovalStatusEnum;
import com.yonyou.crm.bpub.detail.entity.DetailsFlagVO;
import com.yonyou.crm.bpub.detail.entity.DetailsGroupVO;
import com.yonyou.crm.bpub.detail.entity.DetailsItemVO;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.cum.contact.entity.ContactVO;
import com.yonyou.crm.cum.cumprmt.entity.CumprmtVO;
import com.yonyou.crm.cum.cumprmt.service.CumprmtBizServiceImpl;
import com.yonyou.crm.cum.customer.service.CustomerBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class CumprmtDetails extends BaseDetails {

	@Autowired
	CumprmtBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		CumprmtVO obj = service.selectById(id);
		if ((obj != null) && (obj.getId() == null)) {
			obj = null;
		}
		return (T)obj;
	}
	
	/**
	 * 获取详情模板
	 */
	@Override
	public  <T extends Serializable> Map<String, List<DetailsGroupVO>> getLayout(T obj) {
		return null;
	}
	
	@Override
	public <T extends Serializable> List<DetailsGroupVO> getHeader(T obj, Map<String, List<DetailsGroupVO>> layoutList) {
		List<DetailsGroupVO> headerlist = new ArrayList<>();
		
		CumprmtVO vo = (CumprmtVO)obj;
		DetailsGroupVO header = new DetailsGroupVO();
		List<DetailsItemVO> headerData = new ArrayList<>();
		DetailsItemVO item;
		
		item = new DetailsItemVO();
		item.setItemkey("commitTime");
		item.setName("申请时间");
		String commitTime = "";
		try {
			commitTime = formateDate(vo.getCommitTime(), "yyyy-MM-dd HH:mm");
		} catch (CrmMessageException e) {
			e.printStackTrace();
		}
		item.setValue(commitTime);
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("orgName");
		item.setName("申请公司");
		item.setValue(vo.getOrgName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("deptName");
		item.setName("申请部门");
		item.setValue(vo.getDeptName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("applyUserName");
		item.setName("申请人");
		item.setValue(vo.getApplyUserName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("cumName");
		item.setName("客户名称");
		item.setValue(vo.getCumName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("cumFullName");
		item.setName("客户全称");
		item.setValue(vo.getCumFullname());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("approvalStateName");
		item.setName("审批状态");
		item.setValue(vo.getApprovalStateName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("approvalUserName");
		item.setName("最后审批人");
		item.setValue(vo.getApprovalUserName());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("approvalTime");
		item.setName("最后审批时间");
		String approvalTime = "";
		try {
			approvalTime = formateDate(vo.getApprovalTime(), "yyyy-MM-dd HH:mm");
		} catch (CrmMessageException e) {
			e.printStackTrace();
		}
		item.setValue(approvalTime);
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("approvalComment");
		item.setName("审批意见");
		item.setValue(vo.getApprovalComment());
		item.setType("singlelinetext");
		headerData.add(item);
		
		item = new DetailsItemVO();
		item.setItemkey("description");
		item.setName("备注");
		item.setValue(vo.getDescription());
		item.setType("singlelinetext");
		headerData.add(item);
		
		header.setGroupdata(headerData);
		headerlist.add(header);
		
		return headerlist;
	}

	@Override
	public <T extends Serializable> List<DetailsGroupVO> getBody(T obj, Map<String, List<DetailsGroupVO>> layoutList){
		List<DetailsGroupVO> body = new ArrayList<>();
		return body;
	}
	
	@Override
	public <T extends Serializable> List<DetailsFlagVO> getFlag(T obj){
		List<DetailsFlagVO> flags = new ArrayList<>();
		
		return flags;
	}
	
	@Override
	public <T extends Serializable> List<String> getAction(T obj){
		List<String> actions = new ArrayList<>();
		if (obj == null) {
			return actions;
		}
		CumprmtVO vo = (CumprmtVO)obj;
		// 判断该升级单状态，如果是已提交/审批中/已完成，则不能编辑。
		if (vo.getApprovalState() > ApprovalStatusEnum.NOT_COMMIT.getValue()) {
			return actions;
		}
		List<CumprmtVO> cumprmtVOs = service.selectByCumId(vo.getCumId());
		boolean isApprovaling = false;
		for (CumprmtVO cumprmtVO : cumprmtVOs) {
			int approvalState = cumprmtVO.getApprovalState();
			if (approvalState > ApprovalStatusEnum.NOT_COMMIT.getValue()) {
				isApprovaling = true;
				break;
			}
		}
		if (isApprovaling) return actions;
		
		actions.add("edit");
		return actions;
	}

}
